import AntModal from "@/components/pt/dialog/AntModal";
import {
  getBizSearchInfo,
  findMaxSort,
  updateBizSearchInfo,
} from "@/api/biz/bizSearchInfo";
import { listBizSearchInfoDetails } from "@/api/biz/bizSearchInfoDetails";
import storage from "store";
import { ACCESS_TOKEN } from "@/store/mutation-types";
export default {
  name: "CreateForm",
  props: {},
  components: {
    AntModal,
  },
  data() {
    return {
      open: false,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      total: 0,
      id: undefined,
      formTitle: "添加产品信息",
      // 表单参数
      form: {},
      rules: {},
      searchDetailsList: [],
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        uploadStatus: "",
        fileList: [],
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + storage.get(ACCESS_TOKEN) },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API,
      },
    };
  },
  filters: {},
  created() {},
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    handleChange(info) {
      const status = info.file.status;

      if (status === "done") {
        this.$message.success(`${info.file.response.msg}`);
      } else if (status === "error") {
        this.$message.error(`${info.file.name} 文件导入失败！`);
      }
    },
    onClose() {
      this.open = false;
      this.reset();
      this.$emit("ok");
      this.$emit("close");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$emit("ok");
      this.$emit("close");
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
      };
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      /** 获取最大编号 */
      findMaxSort().then((response) => {
        this.form.sort = response.data;
        this.formTitle = "添加产品信息";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.spinning = !this.spinning;
      const bizSearchInfoId = row.id;
      getBizSearchInfo(bizSearchInfoId).then((response) => {
        this.form = response.data;
        this.formTitle = "查看产品信息";
        this.spinning = !this.spinning;

        // 查询列表
        listBizSearchInfoDetails({ infoId: bizSearchInfoId }).then((res) => {
          this.searchDetailsList = res.data.list;
          if (!this.form.detailsId && res.data.list.length > 0) {
            this.form.detailsId = res.data.list[0];
          }
        });
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.open = false;
      const saveForm = JSON.parse(JSON.stringify(this.form));
      if (this.form.id !== undefined) {
        updateBizSearchInfo(saveForm).then((response) => {
          this.$message.success("修改成功", 3);
          this.open = false;
          this.$emit("ok");
          this.$emit("close");
        });
      }
      // this.$emit("ok");
      // this.$emit("close");
    },
    callback(key) {
      this.form.detailsId = key;
      this.form.status = "2";
    },
    back() {
      const index = "/biz/searchinfo/index";
      this.$router.push(index);
    },
  },
};
